﻿using System;
using System.Collections;
using QFramework;
using Sirenix.OdinInspector;
using UnityEngine;

namespace Gp.Scripts.Core {
    public class SkillFx : MonoBehaviour {
        [OnValueChanged("OnParticleChange")]
        public ParticleSystem particle;
        [LabelText("持续时间")]
        [SerializeField]
        public float durationTime;
        
        private void Reset() {
            particle = GetComponent<ParticleSystem>();
            if (particle != null) {
            }
        }


        private float _timeCount = 0;
        private void Update() {
            _timeCount += Time.deltaTime;
            if (_timeCount > durationTime + .1f) {
                Destroy(gameObject);
            }
        }


#if UNITY_EDITOR
        [Button("更新持续时间")]
        private void UpdateDurationTime() {
            if (particle == null) return;
            durationTime = particle.time;
        }

        private void OnParticleChange() {
            if (particle == null) return;
            durationTime = particle.time;
        }
        
#endif
        
        
        public static IEnumerator YieldPlay(SkillFx fx, Vector3 position) {
            if (fx == null) yield break;
            var obj = Instantiate(fx);
            obj.transform.position = position;
            yield return new WaitForSeconds(obj.durationTime);
        }
    }


    public static class SkillFxExtension {
        public static IEnumerator YieldPlay(this SkillFx fx, Vector3 position) {
            if (fx == null) yield break;
            var obj = fx.Instantiate();
            obj.transform.position = position;
            yield return new WaitForSeconds(obj.durationTime);
        }
    }
}